<style scoped src="./index.scss" lang="scss" rel="stylesheet/scss"></style>
<template src="./index.html"></template>
<script>
export default {
    mixins: [Vue.prototype.$vFN.Mixins.Base, Vue.prototype.$vFN.Mixins.Edit],
    components: {
        SelectImage: async () =>
            Vue.extend(await Vue.prototype.$vFN.Components.Base.SelectImage()),
    },
    data() {
        return {
            appWindow: window,
            form: {
                name: "",
                sort: 0,
                searchStatus: 0,
                attributeValueLst: [],
            },

            // 添加时
            value: "",

            // 页面接口地址
            apiUrl: {
                create: "mall-site-portal/product-attribute/create",
                update: "mall-site-portal/product-attribute/update",
                detail: "mall-site-portal/product-attribute/getItem",
            },
        };
    },
    methods: {
        // 添加属性值
        addValue() {
            if (this.value == "") {
                this.$vFN.Tools.messageError("属性值不能为空");
                return;
            }
            let _is = this.form.attributeValueLst.some((item) => {
                return item.attributeValue == this.value;
            });
            if (_is) {
                this.$vFN.Tools.messageError("属性值不能重复");
                return;
            }
            if (this.id) {
                this.addValueSubmit();
            } else {
                this.form.attributeValueLst.push({
                    attributeValue: this.value,
                });
                this.value = "";
            }
        },
        // 删除属性值
        delValue(item_, index_) {
            this.$confirm(
                "确定删除  <strong>" +
                    item_.attributeValue +
                    "</strong>  属性值么?",
                "提示",
                {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    dangerouslyUseHTMLString: true,
                }
            )
                .then(() => {
                    if (this.id) {
                        this.delValueSubmit(item_.id);
                    } else {
                        this.form.attributeValueLst.splice(index_, 1);
                    }
                })
                .catch(() => {});
        },
        // 添加提交
        async addValueSubmit() {
            let _data = await this.$vFN.Http.post(
                {
                    url:
                        "mall-site-portal/product-attribute/createAttributeValue",
                    data: {
                        attributeValue: this.value,
                        attributeId: this.id,
                    },
                },
                window
            );
            if (_data._ok) {
                this.getValueList();
                this.value = "";
            }
        },
        // 删除提交
        async delValueSubmit(id_) {
            let _data = await this.$vFN.Http.post(
                {
                    url: "mall-site-portal/product-attribute/delAttributeValue",
                    data: {
                        ids: [id_],
                    },
                },
                window
            );
            if (_data._ok) {
                this.getValueList();
            }
        },
        // 刷新属性值
        async getValueList() {
            let _data = await this.$vFN.Http.post(
                {
                    url: this.apiUrl.detail,
                    data: {
                        id: this.id,
                    },
                },
                window
            );
            if (_data._ok) {
                this.form.attributeValueLst = _data.data.attributeValueLst;
            }
        },
        // 初始化
        async init() {
            // 初始化数据
            await this.mixinEdit_editBoxInit({
                model: "form",
                id: this.id,
            });
        },

        // form 提交添加/修改 的参数扩展方法 返回false 则不提交用于扩展验证
        mixinEdit_editFormSubmitExtend(data_) {
            console.error(data_);
            if (data_.attributeValueLst.length <= 0) {
                this.$vFN.Tools.messageError("属性值至少存在一个!");
                return false;
            }
            else{
                return data_;
            }
        },
    },
    mounted() {
        this.init();
    },
    activated() {},
};
</script>